package com.amox.uitest.kit;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.amox.uitest.adapter.BaseAdapter;
import com.amox.uitest.adapter.UitestAdapter;
import com.amox.uitest.common.KeyWord;
import com.amox.uitest.tcase.BaseTestCase;

public class XMLKit {

	private final static XMLKit kit = new XMLKit();
	
	private final static String l = "/";

	private static BaseTestCase uitest;
	
	private XMLKit() {
	}

	public static XMLKit me(BaseTestCase uitest) {
		XMLKit.uitest = uitest;
		return kit;
	}

	public Document readAsDoc(File file) {
		Document document = null;
		try {
			document = new SAXReader().read(file);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	public void exec(Document document) throws Exception {
		Node root = document.selectSingleNode(l + KeyWord.uitest);
		BaseAdapter<Boolean> adpater = new UitestAdapter(uitest);
		adpater.play((Element) root);
	}
	
	public Node byAttr(Node node, Object... arguments) {
		return node.selectSingleNode(StrKit.me().format("{0}[@{1}=''{2}'']", arguments));
	}
	
	public boolean isStandard(Document document) {
		Node uitest = document.selectSingleNode(l + KeyWord.uitest.name());
		if(null == uitest) {
			LogKit.printLog(StrKit.me().error(KeyWord.uitest));
			return false;
		}
		String ver = ((Element) uitest).attributeValue(KeyWord.version.name());
		if(null == ver || "".equals(ver)) {
			LogKit.printLog(StrKit.me().error(KeyWord.uitest + "[{0}]"), KeyWord.version);
			return false;
		} else {
			LogKit.printLog(KeyWord.uitest + "[{0}]  = {1}", KeyWord.version, ver);
		}
		
		List<Node> clazzes = uitest.selectNodes(KeyWord.clazz.toString());
		if(null == clazzes || clazzes.isEmpty()) {
			LogKit.printLog(StrKit.me().error(KeyWord.uitest + l + KeyWord.clazz));
			return false;
		}
		
		for(Node clazz : clazzes) {
			List<Node> funcs = clazz.selectNodes(KeyWord.func.name());
			if(null == funcs || funcs.isEmpty()) {
				LogKit.printLog(StrKit.me().error(KeyWord.uitest + l + KeyWord.clazz + l + KeyWord.func));
				return false;
			}
			
			StringBuffer sb = new StringBuffer();
			for(Node func : funcs) {
				String funcName = ((Element) func).attributeValue(KeyWord.id.name());
				sb.append(", " + funcName);
			}
			LogKit.printLog("Found Functions[{0}]", sb.substring(2));
		}
		return true;
	}
	
	public Element closet(Element element, String name) {
		Element parent = element.getParent();
		if(name.equals(parent.getName())) {
			return parent;
		}
		return closet(parent, name);
	}
	
	public String readAsString(File file) throws Exception {
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(file);
			// 拿到第一个用户的密码
			System.out.println("1.------>使用绝对路径方式来查找元素");
			Element element = (Element) document.selectSingleNode("/users/user/password");
			String password = element.getText();
			System.out.println(password);

			System.out.println("2.------>使用相对路径查找元素");
			// element是当前获取的password元素
			Element name = (Element) element.selectSingleNode("../name");
			System.out.println("第一个用户的姓名为" + name.getText());

			System.out.println("3.------>使用全局搜索的方式");
			// 获取所有的id元素的文本
			List<Node> idNodeList = document.selectNodes("//id");
			for (Node node : idNodeList) {
				Element idElement = (Element) node;
				System.out.println(idElement.getText());
			}
			System.out.println("4.------>谓语形式");
			// 获取id=10002的用户信息
			Element idElement = (Element) document.selectSingleNode("//user[@id='10002']");
			List<Element> elements = idElement.elements();
			for (Element element1 : elements) {
				System.out.println(element1.getName() + "=" + element1.getText());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
